package metu.tespit.entitiy.accountManagement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import metu.tespit.entity.course.Course;
import metu.tespit.entity.course.ProjectTeam;


import org.hibernate.validator.Email;
import org.jboss.seam.annotations.security.management.UserPassword;


/**
 
 * @created 01 Nis 2012
 */

@Entity
@Table(name="user", uniqueConstraints = @UniqueConstraint(columnNames = { "username", "email" }))
public class User implements Serializable{

	public static final Long INSTRUCTOR = 1L;
	public static final Long TEACHING_ASSISTANT = 2L;
	public static final Long STUDENT = 3L;
	
	private static final long serialVersionUID = -7361922211051390000L;

	@Id @GeneratedValue
	private Long id;

	private String name;

	@Column(unique = true)
	private String username;

	private String surname;

    @Email
	private String email;

	@ManyToMany(cascade=CascadeType.ALL)
	private List<Course> coursesTaken=new ArrayList<Course>();
	
	@OneToMany(mappedBy="instructor", cascade=CascadeType.ALL)
	private List<Course> instructed=new ArrayList<Course>();
	
	@OneToMany(mappedBy="teachingAssistant", cascade=CascadeType.ALL)
	private List<Course> assisted = new ArrayList<Course>();;
	
	@Lob @Basic(fetch=FetchType.LAZY)
	@Column(length=16777250)
	private byte[] picture;

	@UserPassword(hash="MD5")
	private String password;

	@OneToMany
	private List<UserRole> userRoles= new ArrayList<UserRole>();

	@ManyToOne
	private ProjectTeam projectTeam;

	@Temporal(TemporalType.DATE)
	private Date dueDate;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<UserRole> getUserRoles() {
		return userRoles;
	}

	public void setUserRoles(List<UserRole> userRoles) {
		this.userRoles = userRoles;
	}

	public Date getDueDate() {
		return dueDate;
	}

	public void setDueDate(Date dueDate) {
		this.dueDate = dueDate;
	}

	public ProjectTeam getProjectTeam() {
		return projectTeam;
	}

	public void setProjectTeam(ProjectTeam projectTeam) {
		this.projectTeam = projectTeam;
	}

	public List<Course> getCoursesTaken() {
		return coursesTaken;
	}

	public void setCoursesTaken(List<Course> coursesTaken) {
		this.coursesTaken = coursesTaken;
	}

	public List<Course> getInstructed() {
		return instructed;
	}

	public void setInstructed(List<Course> instructed) {
		this.instructed = instructed;
	}

	public List<Course> getAssisted() {
		return assisted;
	}

	public void setAssisted(List<Course> assisted) {
		this.assisted = assisted;
	}

	public String userRolesString(){

		String result="";

		if(userRoles.size()>0){
			for(UserRole s:userRoles){

				result=result+s.getRoleName()+", ";

			}

			return result.substring(0, result.length()-2);
		}
		else
			return "User Has No Roles!!";

	}

	public String nameToSrting(){

		return name+" "+surname;

	}

	public void setPicture(byte[] picture) {
		this.picture = picture;
	}

	public byte[] getPicture() {
		return picture;
	}
	
	public boolean isInstructor(){
		for (Iterator<UserRole> iterator = getUserRoles().iterator(); iterator.hasNext();) {
			UserRole role = iterator.next();
			if(role.getId().equals(INSTRUCTOR)){
				return true;
			}
		}
		
		return false;
	}
	
	public boolean isStudent(){
		for (Iterator<UserRole> iterator = getUserRoles().iterator(); iterator.hasNext();) {
			UserRole role = iterator.next();
			if(role.getId().equals(STUDENT)){
				return true;
			}
		}
		
		return false;
	}
	
	public boolean isTeachingAssistant(){
		for (Iterator<UserRole> iterator = getUserRoles().iterator(); iterator.hasNext();) {
			UserRole role = iterator.next();
			if(role.getId().equals(TEACHING_ASSISTANT)){
				return true;
			}
		}
		
		return false;
	}

	
	public boolean aktifSemesterdaKursuVarMi() {

		for (Iterator<Course> iterator = getCoursesTaken().iterator(); iterator.hasNext();) {
			Course course = iterator.next();
			if(course.getSemester().verilenTarihdeMi(new Date())){
				return true;
			}
		}
		
		for (Iterator<Course> iterator = getInstructed().iterator(); iterator.hasNext();) {
			Course course = iterator.next();
			if(course.getSemester().verilenTarihdeMi(new Date())){
				return true;
			}
		}
		
		for (Iterator<Course> iterator = getAssisted().iterator(); iterator.hasNext();) {
			Course course = iterator.next();
			if(course.getSemester().verilenTarihdeMi(new Date())){
				return true;
			}
		}
		
		return false;
	}
	

}
